import base from './base';
import vm from '@/main';

export default class menu extends base {
    /**
* [getMenuList 获取菜单列表]
*
* @return  {[type]}  [return description]
*/
    static getMenuList () {
        let url = 'Menu/index';
        return this.get(url).then(data => {
            let lv = 0;
            data.list.map(item => {
                this.addAttr(item, lv);
            });
            return data.list;
        }).catch(error => {
            vm.$Message.error(error.message);
            console.error(error);
        });
    }
    /**
     * [modifyMenu 修改菜单 包括添加/修改]
     *
     * @return  {[type]}  [return description]
     */
    static modifyMenu (url, params) {
        return this.post(url, params, true).then(data => {
            vm.$Message.success(data.msg);
        }).catch(error => {
            vm.$Message.error(error.message);
            console.error(error);
        });
    }
    /**
     * [delMenu 删除菜单]
     *
     * @return  {[type]}  [return description]
     */
    static delMenu (params) {
        let url = 'Menu/del';
        return this.get(url, params, true).then(data => {
            vm.$Message.success(data.msg);
            return 0;
        }).catch(error => {
            vm.$Message.error(error.message);
            console.error(error);
            return 1;
        });
    }
    /**
     * [changeStatus 修改菜单显示状态]
     *
     * @return  {[type]}  [return description]
     */
    static changeStatus (params) {
        let url = 'Menu/changeStatus';
        return this.get(url, params, true).then(data => {
            vm.$Message.success(data.msg);
        }).catch(error => {
            vm.$Message.error(error.message);
            console.error(error);
        });
    }

    /**
   * [addAttr 对不包含子菜单的菜单 增加禁止打开属性]
   *
   * @return  {[type]}  [return description]
   */
    static addAttr (item, lv) {
        item.lv = lv;
        let prefix = '';
        for (let i = 0; i < lv; i++) {
            prefix += '|---';
        }
        item.name = prefix + item.name;
        if (!item.children) {
            item._disableExpand = true;
        } else {
            item.children.map(it => {
                this.addAttr(it, lv + 1);
            });
        }
    }
}
